<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/4/1
 * Time: 15:54
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\SeminarTemplateCategoryGoodsModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class SeminarCategoryGoodsService
 * @package SDXApi\Service
 */
class SeminarCategoryGoodsService extends TemplateAbstract
{
	/**
	 * @var SeminarTemplateCategoryGoodsModel
	 */
	protected $seminarCategoryGoodsModel;

	/**
	 * @var GoodsRepository
	 */
	protected $goodsRepository;

	/**
	 * SeminarCategoryGoodsService constructor.
	 */
	public function __construct()
	{
		$this->seminarCategoryGoodsModel = new SeminarTemplateCategoryGoodsModel();
		$this->goodsRepository = new GoodsRepository();
	}

	/**
	 * Created by Jerry
	 * @param array $template
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	public function handle($template = array(), $userId = 0, $wid = 8)
	{
        $userRank = $this->getUserRank($userId);
		$cacheName = 'seminar_template_page_category_all_' . $template['id'] . '_rank_' . $userRank . '_wid_' . $wid;
		$data = $this->getCache($cacheName);
		if (empty($data)) {
            $data =  $this->categoryGoods($template, $userId, $wid);

            if (! empty($data)) {
                $this->cacheData($cacheName, $data, mt_rand(3600, 4200));
            }
        }

        return $data;
	}

	/**
	 * 处理分类数据
	 * Created by Jerry
	 * @param array $page
	 * @param int $userId
	 * @param int $wid
	 * @return array|bool
	 */
	protected function categoryGoods($page = array(), $userId = 0, $wid = 8)
	{
		if (empty($page)) {
			return false;
		}

		$categoryInfo = $this->findCategoryByStencilId($page['id']);

		$goods = $this->checkGoodsCategory($categoryInfo, $userId, $wid, $page['id'], $page['is_hide_promote']);

		if (empty($goods)) {
			$goods = array();
		}

        if ($page['goods_type'] == 1) {
            $number = count($goods);

            if (($number % 2) == 1) {
                array_pop($goods);
            }
        }


		$page['goods_list'] = $goods;
		$page['type'] = 3;
		unset($page['goods_id']);



		unset($page['font_type'], $page['font_size'], $page['font_content'], $page['font_link'], $page['image_url'], $page['image_link'], $page['image_size'], $page['template_id']);

		return $page;
	}

	/**
	 * Created by Jerry
	 * @param int $id
	 * @return bool|mixed
	 */
	protected function findCategoryByStencilId($id = 0)
	{
		if (empty($id)) {
			return false;
		}

		return $this->seminarCategoryGoodsModel->findCategoryByTemplateId($id);
	}

    /**
     * @author Jerry
     * @param array $category
     * @param int $userId
     * @param int $wid
     * @param int $templateId
     * @param int $is_hide_promote
     * @return array|mixed
     */
	protected function checkGoodsCategory($category = array(), $userId = 0, $wid = 8, $templateId = 0, $is_hide_promote = 0)
	{
		$goodsList = $this->getCategoryGoodsCache($templateId, $category, $userId, $wid, $is_hide_promote);

		foreach ($goodsList as $key => $value) {
			$goodsList[$key]['id'] = $value['goods_id'];
		}

		if (count($goodsList) > 1) {
			$goodsList = self::checkDataSort($goodsList);
		}

		$goodsList = array_merge($goodsList);

		return $goodsList;
	}

    /**
     * @author Jerry
     * @param int $templateId
     * @param array $category
     * @param int $userId
     * @param int $wid
     * @param int $is_hide_promote
     * @return array
     */
	protected function getCategoryGoodsCache($templateId = 0, $category = array(), $userId = 0, $wid = 8, $is_hide_promote = 0)
	{
		$userRank = $this->getUserRank($userId);
		$cacheName = 'seminar_template_page_category_' . $templateId . '_rank_' . $userRank . '_wid_' . $wid;

		$goodsList = array();
		$goodsList = $this->getCache($cacheName);
		$this->goodsRepository->wid = $wid;
		$this->goodsRepository->userId = $userId;

		if (empty($goodsList)) {
			$goods = $this->findCategoryGoodsByCategoryId($category['category_id'], $category['goods_where'], $wid, $is_hide_promote);
			$goodsList = $this->goodsRepository->formatGoodsTransform($goods);

			if (! empty($goodsList)) {
				$this->cacheData($cacheName, $goodsList, mt_rand(3600, 4200));
			}
		}

		return $this->goodsRepository->checkGoodsPractical($goodsList);
	}

    /**
     * @author Jerry
     * @param int $category_id
     * @param int $goods_where
     * @param int $wid
     * @param int $is_hide_promote
     * @return bool|mixed
     */
	protected function findCategoryGoodsByCategoryId($category_id = 0, $goods_where = 0, $wid = 8, $is_hide_promote = 0)
	{
		return $this->goodsRepository->findCategoryGoodsByCategoryId($category_id, $goods_where, $wid, $is_hide_promote);
	}
}